home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Navigation Services SDK / Examples / Sampler / Sampler ƒ / Common.c next >
Encoding:
Text File  |  1999-06-16  |  14.2 KB  |  568 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Common.c
  3.  
  4.     Copyright:    © 1997-1998 by Apple Computer, Inc., all rights reserved.
  5.  
  6. */
  7.  
  8. //    
  9. //    You may incorporate this sample code into your applications
  10. //    without restriction. This sample code has been provided "AS
  11. //    IS" and the responsibility for its operation is 100% yours.
  12. //    You are not permitted to redistribute the source as "Apple
  13. //    sample code" after having made changes. If you're going to
  14. //    re-distribute the source, we require that you make it clear
  15. //    in the source that the code was descended from Apple sample
  16. //    code, but that you've made changes.
  17. //    
  18.  
  19. #pragma segment AppSeg
  20.  
  21. #ifndef __EVENTS__
  22. #include <Events.h>
  23. #endif
  24.  
  25. #ifndef __TRAPS__
  26. #include <Traps.h>
  27. #endif
  28.  
  29. #ifndef __ICONS__
  30. #include <Icons.h>
  31. #endif
  32.  
  33. #ifndef __GESTALT__
  34. #include <Gestalt.h>
  35. #endif
  36.  
  37. #ifndef Common_Defs
  38. #include "Common.h"
  39. #endif
  40.  
  41. extern short gInBackground;
  42.  
  43. // routines used for getting file path names:
  44. OSErr PPrepend(StringPtr dst,StringPtr src);
  45. void PAppend(StringPtr dst, StringPtr src);
  46.  
  47. TrapType GetTrapType(short theTrap);
  48. short NumToolboxTraps(void);
  49.  
  50.  
  51. // ********************************************************************************
  52. // *    ConcatPP()
  53. // ********************************************************************************
  54. unsigned char* ConcatPP(unsigned char* a, unsigned char* b)
  55. {
  56.     short i;    
  57.     for (i = 1;i <= b[0];i++)
  58.         a[++a[0]] = b[i];        
  59.     return (a);
  60. }
  61.  
  62.  
  63. // ********************************************************************************
  64. // *    FSSpecsEq()
  65. // ********************************************************************************
  66. Boolean FSSpecsEq(FSSpec* a,FSSpec* b)
  67. {
  68.     if (a->vRefNum != b->vRefNum)
  69.         return (false);
  70.     if (a->parID != b->parID)
  71.         return (false);
  72.     if (IUEqualString(a->name,b->name))
  73.         return (false);
  74.     return (true);
  75. }
  76.     
  77.     
  78. // ********************************************************************************
  79. // *    rnd()
  80. // *    Returns a 16-bit random number no large than 'max'.
  81. // ********************************************************************************
  82. static SInt16 rnd(SInt16 max)
  83. {
  84.     return ((UInt32) max * (UInt16) Random()) >> 16;
  85. }
  86.  
  87.  
  88. // ********************************************************************************
  89. // *    RandTween()
  90. // *    Returns an 8-bit number between low and high.
  91. // ********************************************************************************
  92. short RandTween(short low, short high)
  93. {
  94.     short result;
  95.     
  96.     result = Random() % (high + 1 - low);
  97.     if (result < 0)
  98.         result = -result;
  99.     result += low;
  100.     return(result);
  101. }
  102.     
  103.  
  104. // ********************************************************************************
  105. // *    GetItemStr()
  106. // ********************************************************************************
  107. unsigned char* GetItemStr(DialogPtr theDialog, short theItem, unsigned char* theString)
  108. {
  109.     Handle ahandle;
  110.     Rect arect;
  111.     short atype;
  112.     
  113.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  114.     GetDialogItemText(ahandle,theString);
  115.     return (theString);
  116. }
  117.  
  118.  
  119. // ********************************************************************************
  120. // *    PokeItemStr()
  121. // ********************************************************************************
  122. void PokeItemStr(DialogPtr theDialog, short theItem, unsigned char *theString)
  123. {
  124.     Handle     ahandle;
  125.     Rect     arect;
  126.     short     atype;
  127.     
  128.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  129.     SetDialogItemText(ahandle,theString);
  130. }
  131.  
  132.  
  133. // ********************************************************************************
  134. // *    PokeCtlVal()
  135. // ********************************************************************************
  136. void PokeCtlVal(DialogPtr theDialog, short theItem, short value)
  137. {
  138.     Handle     ahandle;
  139.     Rect     arect;
  140.     short    atype;
  141.     
  142.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  143.     SetControlValue((ControlHandle)ahandle,value);
  144. }
  145.  
  146.  
  147. // ********************************************************************************
  148. // *    PokeCtlHilite()
  149. // ********************************************************************************
  150. void PokeCtlHilite(DialogPtr theDialog, short theItem, short value)
  151. {
  152.     Handle     ahandle;
  153.     Rect     arect;
  154.     short     atype;
  155.     
  156.     GetDialogItem(theDialog,theItem,&atype,&ahandle,&arect);
  157.     HiliteControl((ControlHandle)ahandle,value);
  158. }
  159.  
  160.  
  161. // ********************************************************************************
  162. // *    MyP2CCopy()
  163. // ********************************************************************************
  164. char* MyP2CCopy(unsigned char* psrc, char* ctarget)
  165. {
  166.     short i;
  167.     for(i = 0;i < psrc[0];i++)
  168.         ctarget[i] = psrc[i + 1];    
  169.     ctarget[i] = 0;
  170.     return (ctarget);
  171. }
  172.  
  173.  
  174. // ********************************************************************************
  175. // *    MyC2PStr()
  176. // ********************************************************************************
  177. unsigned char* MyC2PStr(char* theStr)
  178. {
  179.     short i,length;
  180.  
  181.     for(i = 0;theStr[i];i++)
  182.         ;
  183.     length = i;
  184.     while(i)
  185.         {
  186.         theStr[i] = theStr[i - 1];
  187.         i--;
  188.         }
  189.     ((unsigned char*)theStr)[0] = length;
  190.     return ((unsigned char *)theStr);
  191. }
  192.  
  193.  
  194. // ********************************************************************************
  195. // *    MyP2CStr()
  196. // ********************************************************************************
  197. char* MyP2CStr(unsigned char* theStr)
  198. {
  199.     short i,length;
  200.     length = theStr[0];
  201.     for(i = 0;i < length;i++)
  202.         theStr[i] = theStr[i + 1];
  203.     theStr[length] = 0;
  204.     return ((char *)theStr);
  205. }
  206.  
  207.  
  208. // ********************************************************************************
  209. // *    DebugCStr()
  210. // ********************************************************************************
  211. void DebugCStr(char* msg)
  212. {
  213.     MyC2PStr(msg);
  214.     DebugStr((unsigned char*)msg);
  215.     MyP2CStr((unsigned char*)msg);
  216. }
  217.  
  218.  
  219. // ********************************************************************************
  220. // *    Str2OSType()
  221. // *    C strings only.
  222. // ********************************************************************************
  223. OSType Str2OSType(Str255 theStr)
  224. {
  225.     OSType num = 0;
  226.     num =  theStr[3] + (theStr[2] << 8) + (theStr[1] << 16) + (theStr[0] << 24);
  227.     return num;    
  228. }
  229.  
  230. // ********************************************************************************
  231. // *    MyStrLen()
  232. // *    C strings only.
  233. // ********************************************************************************
  234. long MyStrLen(char* s)
  235. {
  236.     long count=0;
  237.     while (*s++ != '\0') count++;
  238.     return count;
  239. }
  240.  
  241.  
  242. // ********************************************************************************
  243. // *    myStringToLong()
  244. // *    C strings only.
  245. // ********************************************************************************
  246. long myStringToLong(char* s)
  247. {
  248.     long n = 0;
  249.     short i;
  250.     
  251.     for (i=0;s[i]>='0' && s[i]<='9';i++)
  252.         n = 10*n + (s[i]-'0');
  253.     return n;
  254. }
  255.  
  256.  
  257. // ********************************************************************************
  258. // *    myStringToShort()
  259. // *    C strings only.
  260. // ********************************************************************************
  261. short myStringToShort(char* s)
  262. {
  263.     short n = 0;
  264.     short i;
  265.     
  266.     for(i=0;s[i]>='0' && s[i]<='9';i++)
  267.         n = 10*n + (s[i]-'0');
  268.     return n;
  269. }
  270.  
  271.  
  272. // ********************************************************************************
  273. // *    myStrCpy()
  274. // *    C strings only.
  275. // ********************************************************************************
  276. void myStrCpy(char* dst, char* src)
  277. {
  278.     while(*src != '\0')
  279.         *dst++ = *src++;
  280.     *dst = '\0';
  281. }
  282.  
  283.  
  284. // ********************************************************************************
  285. // *    myStrCat()
  286. // ********************************************************************************
  287. void myStrCat(char* dst, char* src)
  288. {
  289.     dst = dst + MyStrLen(dst);
  290.     myStrCpy(dst,src);
  291. }
  292.  
  293.  
  294. // ********************************************************************************
  295. // *    flag routines for KeyDown events..
  296. // ********************************************************************************
  297. Boolean OptionDown()    { return ModifierDown(optionKey); }
  298. Boolean ShiftDown()     { return ModifierDown(shiftKey); }
  299. Boolean CommandDown()    { return ModifierDown(cmdKey); }
  300. Boolean ControlDown()    { return ModifierDown(controlKey); }
  301.  
  302.  
  303. // *****************************************************************************
  304. // *
  305. // *    ModifierDown()
  306. // *
  307. // *****************************************************************************
  308. Boolean ModifierDown(short modifier)
  309. {
  310.     EventRecord    event;
  311.     (void) OSEventAvail(0,&event);
  312.     return ((event.modifiers & modifier) != 0);
  313. }
  314.  
  315.  
  316. // *****************************************************************************
  317. // *
  318. // *    hiliteTheButton()
  319. // *
  320. // *****************************************************************************
  321. void hiliteTheButton(DialogPtr theDialog, short whichItem)
  322. {
  323.     short        itemType = 0;
  324.     Handle        itemHdl;
  325.     Rect        itemRect;
  326.     long int     startTick;
  327.  
  328.     GetDialogItem(theDialog,whichItem,&itemType,&itemHdl,&itemRect);
  329.     HiliteControl((ControlHandle)itemHdl,1);
  330.     for (startTick=TickCount();TickCount()-startTick<kDelayTick;)
  331.         {} // do nothing...
  332.     HiliteControl((ControlHandle)itemHdl,kActive);
  333. }
  334.  
  335.  
  336. // *****************************************************************************
  337. // *
  338. // *    AdornButton()
  339. // *    
  340. // *****************************************************************************
  341. void AdornButton(DialogPtr theDialog, short whichItem)
  342. {
  343.     GrafPtr    oldPort;
  344.     Handle    itemH;
  345.     short    itemType;
  346.     Rect    itemRect;    
  347.     long     version = 0;
  348.     OSErr    theErr = noErr;
  349.  
  350.     theErr = Gestalt(gestaltSystemVersion,&version);
  351.     if (version <= 0x0760)        // this look is only for Systems after 7.6.0
  352.         {
  353.         GetPort(&oldPort);
  354.         
  355.         SetPort(theDialog);
  356.         GetDialogItem(theDialog,whichItem,&itemType,&itemH,&itemRect);
  357.         PenSize(3,3);
  358.         InsetRect(&itemRect,-4,-4);
  359.         FrameRoundRect(&itemRect,14,14);
  360.         
  361.         PenNormal();
  362.         SetPort(oldPort);
  363.         }
  364. }
  365.  
  366.  
  367. // *****************************************************************************
  368. // *
  369. // *    DrawIconSuite()
  370. // *
  371. // *    This routine draws the appropriate icon into 'destRect' based on the
  372. // *    'resID' of the icon suite.  The caller must set its port before calling
  373. // *    this routine.
  374. // *    
  375. // *****************************************************************************
  376. void DrawIconSuite(short resID, Rect destRect)
  377.     IconSelectorValue    iconType;
  378.     IconAlignmentType    align;
  379.     IconTransformType    transform;
  380.     OSErr                theErr = noErr;
  381.     Handle                iconSuiteHdl;
  382.     
  383.     iconType = svAllAvailableData;
  384.     theErr = GetIconSuite(&iconSuiteHdl,resID,iconType);
  385.     if ((theErr == noErr) && (iconSuiteHdl != nil))
  386.         {
  387.         align = atAbsoluteCenter;
  388.         transform = ttNone;
  389.         theErr = PlotIconSuite(&destRect,align,transform,iconSuiteHdl);
  390.         }
  391. }
  392.  
  393.  
  394. // ********************************************************************************
  395. // *
  396. // *    PAppend()
  397. // *
  398. // ********************************************************************************    
  399. void PAppend(StringPtr dst, StringPtr src)
  400. {
  401.     BlockMove(src + 1, dst + *dst + 1, *src);
  402.     *dst += *src;
  403. }
  404.  
  405.  
  406.  
  407. // ********************************************************************************
  408. // *
  409. // *    PPrepend()
  410. // *        or "Path Append": add 'src' to the end of 'dst'.
  411. // *    - Pascal strings only.
  412. // *
  413. // ********************************************************************************    
  414. OSErr PPrepend(StringPtr dst,StringPtr src)
  415. {
  416.     if(*dst + *src > 255)
  417.         return(-1);
  418.     
  419.     BlockMove(dst + 1, dst + *src + 1, *dst);
  420.     BlockMove(src + 1, dst + 1, *src);
  421.     *dst += *src;
  422.     
  423.     return(0);
  424. }
  425.  
  426.  
  427. // ********************************************************************************
  428. // *
  429. // *    PathNameFromDirID()
  430. // *
  431. // *    Creates a full or partial path name of a given file.
  432. // *    - Inputs 'dirID' and 'vRefNum' and outputs 'fullPathName'.
  433. // *    - fullPathLength = num characters requested.
  434. // *    - full = flag to make full path, rather than just the item name.
  435. // *
  436. // ********************************************************************************    
  437. OSErr PathNameFromDirID(long dirID,
  438.                         short vRefNum,
  439.                         StringPtr fullPathName,
  440.                         short maxPathLength,
  441.                         short full)
  442. {
  443.     DirInfo    block;
  444.     Str255    directoryName;
  445.     OSErr     theErr;
  446.  
  447.     fullPathName[0] = 0;
  448.  
  449.     block.ioDrParID = dirID;
  450.     block.ioNamePtr = directoryName;
  451.     do {
  452.         block.ioVRefNum = vRefNum;
  453.         block.ioFDirIndex = -1;
  454.         block.ioDrDirID = block.ioDrParID;
  455.         theErr = PBGetCatInfoSync((CInfoPBPtr)&block);
  456.         if (theErr)
  457.             return(theErr);
  458.         
  459.         PAppend(directoryName,(StringPtr)"\p:");
  460.         
  461.         if (fullPathName[0] + directoryName[0] >= maxPathLength)
  462.             return(-1);    // actual path name too long for the requested path name size
  463.             
  464.         PPrepend(fullPathName,directoryName);
  465.         }
  466.     while ((block.ioDrDirID != 2) && (full));
  467.         
  468.     return(0);
  469. }
  470.  
  471.  
  472. // *****************************************************************************
  473. // *
  474. // *    AdjustCursor()
  475. // *
  476. // *****************************************************************************
  477. void AdjustCursor(Point theLoc, RgnHandle theRgn)
  478. {    
  479.     WindowPtr    theWindow;
  480.     Document*    theDocument;
  481.     RgnHandle    arrowRgn, iBeamRgn, hiliteRgn;
  482.     Rect        theRect;
  483.     Point        thePoint;
  484.  
  485.     if (gInBackground)
  486.         return;
  487.  
  488.     arrowRgn = NewRgn();
  489.     SetRectRgn(arrowRgn,-32767,-32767,32767,32767);
  490.  
  491.     if ((theWindow = FrontWindow()) &&
  492.         (theDocument = IsDocumentWindow(theWindow)) &&
  493.         (theDocument->theTE != NULL))
  494.         {
  495.         SetPort(theWindow);
  496.  
  497.         iBeamRgn = NewRgn();
  498.         hiliteRgn = NewRgn();
  499.  
  500.         theRect = (**(theDocument->theTE)).viewRect;
  501.         LocalToGlobal((Point*)&(theRect.top));
  502.         LocalToGlobal((Point*)&(theRect.bottom));
  503.         RectRgn(iBeamRgn,&theRect);
  504.  
  505.         CopyRgn(theDocument->hiliteRgn,hiliteRgn);
  506.         thePoint.h = thePoint.v = 0;
  507.         LocalToGlobal(&thePoint);
  508.         OffsetRgn(hiliteRgn,thePoint.h,thePoint.v);
  509.  
  510.         DiffRgn(arrowRgn,hiliteRgn,arrowRgn);
  511.         DiffRgn(arrowRgn,iBeamRgn,arrowRgn);
  512.  
  513.         DiffRgn(iBeamRgn,hiliteRgn,iBeamRgn);
  514.  
  515.         if (PtInRgn(theLoc,iBeamRgn))
  516.             {
  517.             SetCursor(*GetCursor(iBeamCursor));
  518.             CopyRgn(iBeamRgn,theRgn);
  519.             }
  520.         else
  521.             {
  522.             if (PtInRgn(theLoc,hiliteRgn))
  523.                 {
  524.                 SetCursor(&qd.arrow);
  525.                 CopyRgn(hiliteRgn,theRgn);
  526.                 }
  527.             else
  528.                 {
  529.                 SetCursor(&qd.arrow);
  530.                 CopyRgn(arrowRgn,theRgn);
  531.                 }
  532.             }
  533.         
  534.         DisposeRgn(iBeamRgn);
  535.         DisposeRgn(hiliteRgn);
  536.         }
  537.     else
  538.         {
  539.         SetCursor(&qd.arrow);
  540.         CopyRgn(arrowRgn,theRgn);
  541.         }
  542.  
  543.     DisposeRgn(arrowRgn);
  544. }
  545.  
  546.  
  547. OSErr AEGetDescData(const AEDesc *desc, DescType *typeCode, void *dataBuffer, ByteCount maximumSize, ByteCount *actualSize)
  548. {
  549.     *typeCode = desc->descriptorType;
  550.     Handle h = (Handle)desc->dataHandle;
  551.     ByteCount dataSize = GetHandleSize(h);
  552.     if (dataSize > maximumSize)
  553.         *actualSize = maximumSize;
  554.     else
  555.         *actualSize = dataSize;
  556.     BlockMoveData(*h, dataBuffer, *actualSize);
  557.     return noErr;
  558. }
  559.  
  560. void DoLowMemAlert( )
  561. {
  562.     Str255 message;
  563.     GetIndString( message, rAppStringsID, sLowMemoryErr );
  564.     ParamText( (ConstStr255Param)message, "\p", "\p", "\p" );
  565.     InitCursor( );
  566.     (void)NoteAlert( rGenericAlertID, 0L );
  567. }